package Test;

import java.util.*;

public class Main05 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        int m = input.nextInt();
        Map<Integer, Integer> map = new TreeMap<Integer, Integer>();
        for (int i = 0; i < n; i++) {
            int key = input.nextInt();
            int value = input.nextInt();
            map.put(key, value);
        }
        System.out.println(function(map, n, m));
    }

    public static int function(Map map, int n, int m) {
        int num = 0;
        Set<Integer> keySet = map.keySet();
        int num1 = 0;
        for (Integer key : keySet) {
            if (key == 1) {
                //这是投一号的人数
                num1++;
                map.remove(key);
            }
        }
        List<Integer> list = new ArrayList<Integer>();
        for (Integer key : keySet) {
            list.add((Integer) map.get(key));
        }
        Collections.sort(list);
        for (int i = 0; i < (n / m + 1 - num1); i++) {
            num += list.get(i);
        }
        return num;
    }

}

